<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
</head>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #03a9f4;position:fixed;" id="header">
        <a class="aui-pull-left aui-btn" tapmode onclick="closeWin();">
            <span class="aui-iconfont aui-icon-left"></span>
        </a>
        <div class="aui-title" id="title">
            注塑投料
        </div>
    </header>
    <div class="aui-bar aui-bar-nav sub-header">
    </div>
    <div class="aui-content aui-margin-b-15" style="margin-top:50px;">
        <div class="white_background">
            <ul class="aui-list aui-form-list">


                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">料桶二维码</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="扫描 料桶二维码" id="workCellCode" value=""
                                onclick="getIdSelector();" />
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">箱码</div>
                        <div class="aui-list-item-input">
                            <input type="text" placeholder="请扫描箱号" id="boxnum" value="" onclick="getIdSelector();">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item" style="display:none">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">条码</div>
                        <div class="aui-list-item-input">
                            <input type="text" id="tm">
                        </div>
                    </div>
                </li>
                <li class="aui-list-item">
                    <div class="aui-list-item-inner">
                        <div class="aui-list-item-label">投料数量</div>
                        <div class="aui-list-item-input">
                            <input type="number" id="qty" placeholder="请输入投料数量">
                        </div>
                    </div>
                </li>
            </ul>
        </div>
        <div class="aui-btn aui-btn-info aui-btn-block" id="submitButton" onclick="save()">投料</div>
        <div class="white_background">
            <div style="background-color:white; height: 220px;width: 100%;margin-top:3%;border:solid 1px #D3D3D3;display:block"
                id="tableDiv">
                <table class="table" style='width:100%; font-size:14px;' id="table">
                    <tbody>
                        <tr>
                            <td style="width:23%;text-align:right;">提&nbsp&nbsp示&nbsp信&nbsp息:</td>
                            <td id="message"></td>
                        </tr>
                        <tr>
                            <td style="width:23%;text-align:right;">
                                箱&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp号:</td>
                            <td id="boxnumT"></td>
                        </tr>
                        <tr>
                            <td style="width:23%;text-align:right;">投&nbsp&nbsp料&nbsp数&nbsp量:</td>
                            <td id="qtyT"></td>
                        </tr>
                        <tr>
                            <td style="width:23%;text-align:right;">物&nbsp&nbsp料&nbsp编&nbsp码:</td>
                            <td id="mrlCodeT"></td>
                        </tr>
                        <tr>
                            <td style="width:23%;text-align:right;">批&nbsp&nbsp&nbsp次&nbsp&nbsp&nbsp&nbsp号:</td>
                            <td id="lotCodeT"></td>
                        </tr>
                        <tr>
                            <td style="width:23%;text-align:right;">源&nbsp&nbsp&nbsp库&nbsp&nbsp&nbsp&nbsp位:</td>
                            <td id="workCellCodeT"></td>
                        </tr>
                        <tr>
                            <td style="width:23%;text-align:right;">目&nbsp&nbsp标&nbsp库&nbsp位:</td>
                            <td id="relWorkCellCodeT"></td>
                        </tr>

                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <footer class="aui-bar aui-bar-tab" id="footer">
        <div class="aui-bar-tab-item" tapmode id="scan" onclick="doScan()">
            <i class="aui-iconfont aui-icon-scan aui-text-epblue" style="font-size: 1.8rem"></i>
        </div>
    </footer>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript" src="../script/clickThrottle.js"></script>
<script type="text/javascript">
    //光标所在id
    var idSelector = "";
    var workCenterGid = ""; //仓库GID
    var workCenterCode = ""; //仓库编码
    var workCenterName = ""; //仓库名称
    var workCellGid = ""; //库位GID
    var workCellCode = ""; //库位编码
    var workCellName = ""; //库位名称

    var scanRule = ""; //条码解析规则

    var yQyt = "0"; //菜单路由参数
    var code; //菜单路由参数
    var codesub; //菜单路由参数
    var characteristic = "";
    //初始化查询仓库下拉框
    apiready = function () {
        $("#workCellCode").focus();
        idSelector = "workCellCode";
        //菜单参数
        var query = api.pageParam.query;
        code = query.code
        codesub = query.codesub
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //查询扫描规则
    function initScanRule() {
        var serverUrl = localStorage.getItem('serverUrl');
        var un = localStorage.getItem("un");
        var url = serverUrl + "/app/getTmScanRule";
        var data = {
            values: {
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    scanRule = myobj.scanRule
                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            }
        });
    }

    //扫描
    function doScan() {
        var moduleScanner = api.require('moduleScanner');
        var params = {
            top: 150, //扫描窗口的上边距
            width: 250, //扫描窗口的宽
            height: 250, //扫描窗口的高
            useColor: false, //是否使用自己设定的颜色(startColor和endColor),
            //如果不使用设定的颜色就使用默认的那张网格图
            startColor: '#0F08C8FF', //渐变起始颜色
            endColor: '#7F08C8FF', //渐变结束颜色
            animTime: 2200, //扫描动画时间,单位为毫秒
        };
        moduleScanner.toScannerActivityForResult(params, function (ret, err) {
            //alert(ret);扫码的结果都是编码
            //1：仓库
            if (idSelector == 'workCellCode') {

                $("#workCellCode").val(ret);
                //查询库位
                getWorkCell(ret, idSelector);

                //扫描条码
            } else if (idSelector == 'boxnum') {
                $("#boxnum").blur();
                tmSplit(ret);
            }
        });
    }



    //5:库位扫描校验
    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: workCenterGid, //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#boxnum").focus();
                    idSelector = "boxnum";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }


    function backOneByte(value) {
        if (value.length >= 4) {
            //console.log(value);
            value = value.substring(value.length - 4, value.length);
            var firstCase = value.substring(0, 1);
            if (firstCase == "0") {
                value = value.substring(value.length - 2, value.length);
            }
            //console.log(value);
        } else if (value.length == 3) {
            //console.log(value);
            value = value.substring(value.length - 2, value.length);
            //console.log(value);
        } else if (value.length == 2) {

        } else if (value.length == 1) {
            value = "0" + value;
        }
        return value;
    }

    function calculateHexSum(str) {
        let sum = 0;
        for (let i = 0; i < str.length; i += 2) {
            const hex = parseInt(str.substr(i, 2), 16);
            sum += hex;
        }
        return sum;
    }

    function zeroPadding1(num, length) {
        var len = num.toString().length;
        while (len < length) {
            num = "0" + num;
            len++;
        }
        return num;
    }




    function getWorkCell(code, inputId) {
        var un = localStorage.getItem("un");
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getWorkCell";
        var data = {
            values: {
                flag: "in_rk", //库内库间标识（out_kn库内  out_kj库间,in_rk入库出库）
                workCenterGid: workCenterGid, //仓库GID
                workCellCode: code, //库位编码
                relWorkCellGid: "", //调入库位
                tm: "",
                workCellFlag: "", //原库位调入库位flag
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == '0') {
                    var list = eval(myobj.list);
                    workCellGid = list[0].workCellGid;
                    workCellCode = list[0].workCellCode;
                    workCellName = list[0].workCellName;
                    document.getElementById(inputId).value = list[0].workCellCode + ":" + list[0].workCellName;
                    $("#boxnum").focus();
                    idSelector = "boxnum";

                } else {
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    $("#workCellCode").focus();
                    $("#workCellCode").select();
                }
            }
        });
    }




    //7；PDA自带扫描回车事件-库位
    $("#workCellCode").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var sm = $(this).val();
        if (keycode == '13') {
            //仓库编码
            $("#workCellCode").val(sm);
            //校验改箱码是否存在
            getWorkCell(sm, "workCellCode");
        }
    });



    //保存按钮
    function save() {

        if (!clickThrottle()) {
            return;
        }



        // //返回成功
        // if (myobj.status === true) {
        //数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //箱号
        var boxnum = $("#boxnum").val();

        //请扫描料桶二维码非空校验
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            api.toast({
                msg: '请扫描料桶二维码',
                duration: 3000
            });
            return;
        }

        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '箱码不能为空',
                duration: 3000
            });
            return;
        }

        //数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            api.toast({
                msg: '请输入投料数量',
                duration: 3000
            });
            return;
        } else {
            if (parseFloat(yQyt) < parseFloat(qty)) {
                api.toast({
                    msg: '投料数量不能大于可投料数量！',
                    duration: 3000
                });
                return;
            }
        }
        saveS()
    }

    function saveS() {
        //数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //箱号
        var boxnum = $("#boxnum").val();

        //请扫描料桶二维码非空校验
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            api.toast({
                msg: '请扫描料桶二维码',
                duration: 3000
            });
            return;
        }

        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            api.toast({
                msg: '箱码不能为空',
                duration: 3000
            });
            return;
        }

        //数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            api.toast({
                msg: '请输入投料数量',
                duration: 3000
            });
            return;
        } else {
            if (parseFloat(yQyt) < parseFloat(qty)) {
                api.toast({
                    msg: '投料数量不能大于可投料数量！',
                    duration: 3000
                });
                return;
            }
        }
        const submitButton = document.getElementById('submitButton');
        submitButton.style.pointerEvents = "none"; // 禁用提交按钮

        api.showProgress({
            title: '保存中...',
        });
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/injectionMolding";
        var data = {
            values: { ////以表单方式提交参数（JSON对象）
                businessType: code, //业务类型
                subBussinessType: codesub, // 子业务类型
                workCellGid: workCellGid, //库位编码
                tm: tm, //条码
                qty: qty,
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {
                    submitButton.style.pointerEvents = "auto"; // 启用提交按钮

                    api.hideProgress();
                    api.toast({
                        msg: "保存成功",
                        duration: 3000
                    });

                    $("#boxnum").val("");
                    $("#workCellCode").val("");
                    $("#qty").val("");

                    var map = myobj.data;
                    for (var key in map) {
                        $("#" + key).html(map[key]);
                    }
                    yQyt = myobj.yqty;
                    console.log(yQyt)
                    $("#message").html(myobj.message);
                    $("#workCellCode").focus();
                    idSelector = "workCellCode";
                } else if (myobj.code == '1') {
                    submitButton.style.pointerEvents = "auto"; // 启用提交按钮
                    $("#boxnum").val("");
                    $("#workCellCode").val("");
                    $("#qty").val("");
                    idSelector = "workCellCode";
                    $("#workCellCode").select();
                    api.hideProgress();
                    clearTabltDiv(myobj.message);
                    api.toast({
                        msg: myobj.message,
                        duration: 5000
                    });
                }
            } else {
                submitButton.style.pointerEvents = "auto"; // 启用提交按钮
                $("#boxnum").val("");
                $("#workCellCode").val("");
                $("#qty").val("");
                $("#workCellCode").select();
                idSelector = "workCellCode";
                api.hideProgress();
                alert(JSON.stringify(err));
            }
        });
    }


    //清空
    function clearTabltDiv(message) {
        $("#message").html("");
        $("#boxnumT").html("");
        $("#qtyT").html("");
        $("#mrlCodeT").html("");
        $("#lotCodeT").html("");
        $("#workCellCodeT").html("");
        $("#relWorkCellCodeT").html("");
    }

    //自动保存
    function saveAuto() {
        //数量
        var qty = $("#qty").val();
        //条码
        var tm = $("#tm").val();
        //箱号
        var boxnum = $("#boxnum").val();

        //请扫描料桶二维码非空校验
        if (workCellCode == "" || workCellCode == null || workCellCode == undefined) {
            return;
        }

        //箱码非空校验
        if (boxnum == "" || boxnum == null || boxnum == undefined) {
            return;
        }

        //数量非空校验
        if (qty == "" || qty == null || qty == undefined) {
            return;
        } else {
            if (parseFloat(yQyt) > parseFloat(qty)) {
                return;
            }
        }

        //校验通过保存
        save();
    }

    //扫描之后条码解析
    function tmSplit(sm) {
        $("#tm").val(sm);

        postAnalyze(sm);

    }

    //本地解析
    function localAnalyze(sm) {
        var arr = sm.split(",");
        if (arr[1] == null || arr[1] == undefined) {
            $("#mrlCode").val("");
        } else {
            $("#mrlCode").val(arr[1]);
            $("#mrlCodeT").html(arr[1]);
        }
        if (arr[2] == null || arr[2] == undefined) {
            $("#qty").val("");
        } else {
            yQyt = arr[2]
            $("#qtyT").html(arr[2]);
        }
        if (arr[3] == null || arr[3] == undefined) {
            $("#lotCode").val("");
        } else {
            $("#lotCode").val(arr[3]);
            $("#lotCodeT").html(arr[3]);
        }
        if (arr[4] == null || arr[4] == undefined) {
            $("#tmID").val("");
        } else {
            $("#tmID").val(arr[4]);
        }
        if (arr[9] == null || arr[9] == undefined) {
            $("#boxnum").val("");
        } else {
            $("#boxnum").val(arr[9]);
            $("#boxnumT").html(arr[9]);
        }

    }

    //后台解析条码
    function postAnalyze(sm) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/analysisBarCode";
        var data = {
            values: {
                tm: sm, //条码
                un: un
            }
        };
        api.ajax({
            url: url,
            method: 'post',
            dataType: 'json',
            timeout: 300,
            returnAll: false,
            data: data
        }, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == '0') {

                    var arr = sm.split(",");
                    $("#mrlCode").val(myobj.mrlCode);
                    $("#mrlCodeT").html(myobj.mrlCode);
                    yQyt = myobj.qty
                    $("#qtyT").html(myobj.qty);
                    $("#lotCode").val(myobj.lotCode);
                    $("#lotCodeT").html(myobj.lotCode);
                    $("#tmID").val(myobj.tmID);
                    $("#boxnum").val(myobj.boxnum);
                    $("#boxnumT").html(myobj.boxnum);
                    //自动保存
                    //saveAuto();
                } else if (myobj.code == '1') {

                    $("#boxnum").val("");
                    $("#boxnum").select();
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert(JSON.stringify(err));
            }
        });
    }

    //箱号监听回车事件
    $("#boxnum").on('keypress', function (e) { //#keyword为input文本框
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#boxnum").blur();
                console.log(tm);
                // $("#boxnum").select();
                //调用条码解析+保存事件
                tmSplit(tm);
            }
        }
    });

    //返回按钮
    function closeWin() {
        api.closeWin({});
    }
</script>

</html>